package com.peng.leetcode.other;

import java.util.Deque;
import java.util.LinkedList;

/**
 * MyQueue
 * <p>
 * 232. 用栈实现队列
 *
 * @author: lupeng6
 * @create: 2021/3/5 10:19
 */
public class MyQueue {

    private Deque<Integer> inStack = new LinkedList<>();

    private Deque<Integer> outStack = new LinkedList<>();

    /**
     * Initialize your data structure here.
     */
    public MyQueue() {

    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        inStack.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        if (outStack.isEmpty()) {
            inOut();
        }
        return outStack.pop();
    }

    /**
     * Get the front element.
     */
    public int peek() {
        if (outStack.isEmpty()) {
            inOut();
        }
        return outStack.peek();
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    private void inOut() {
        while (!inStack.isEmpty()) {
            outStack.push(inStack.pop());
        }
    }
}
